function addSomain() {
    show("/so/somainAdd.html")
}
function searchSo(obj) {
    window.sessionStorage.setItem("soId",obj)
    show("/so/somainSearch.html")
}
function backSo() {
    show("/so/somain.html")
}
function addSoitem() {
    var tr = $('<tr></tr>');
    $('#soitemtablelist').append(tr);
    tr.append('<td>'+tr.index()+'</td>'
        +'<td><input type="text" name="productCode" class="poinput" readonly/><img src="/img/编辑.png" style="width: 20px;height: 20px" onclick="selectProduct(this)" /></td>'
        +'<td><input type="text" name="name" class="poinput" readonly /></td>'
        +'<td><input type="text" name="unitName" class="poinput" readonly /></td>'
        +'<td><input type="text" name="num" class="poinput" /></td>'
        +'<td><input type="text" name="" class="poinput" /></td>'
        +'<td><input type="text" name="unitPrice" class="poinput"/></td>'
        +'<td><input type="button" class="point" value="删除" onclick="deleteItem(this)"/></td>'
    );
}
function selectCustomer(obj) {
    Ajax.getHtml('so/selectCustomer.html', function (html) {
        layer.open({
            type: 1,
            title: '请选择供货商名称',
            area: ['600px'],
            content: html,
            btn: ['确认', '取消'],
            yes: function () {
                obj.parentElement.children[0].value=$("input[type=radio]:checked").val()
                layer.closeAll()
            },
            btn2: function () {
                console.log('取消')
            }
        })
    })

}

function selectProduct(obj) {
    Ajax.getHtml('po/selectProduct.html', function (html) {
        layer.open({
            type: 1,
            title: '请选择产品名称',
            area: ['600px'],
            content: html,
            btn: ['确认', '取消'],
            yes: function () {
                backProduct(obj)
            },
            btn2: function () {
                console.log('取消')
            }
        })
    })
}
function backProduct(obj) {
    Ajax.post(
        '/selectProductByName',
        {
            name:$("input[type=radio]:checked").val()
        },
        function (result) {
            doResult(result, function () {
                layer.closeAll()
                obj.parentElement.children[0].value=result.data.productCode
                obj.parentElement.parentElement.children[2].children[0].value=result.data.name
                obj.parentElement.parentElement.children[3].children[0].value=result.data.unitName
                obj.parentElement.parentElement.children[6].children[0].value=result.data.price
            })
        }
    )
}
function deleteItem(obj) {
    var tr = $(obj).parents('tr');
    var index = tr.index()-1;
    tr.remove();
    $('#soitemtablelist tr:gt('+index+')').each(function(){
        $(this).find('td:first').text($(this).index());
    });
}
function saveSomain() {
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, // 月份
            "d+": this.getDate(), // 日
            "h+": this.getHours(), // 小时
            "m+": this.getMinutes(), // 分
            "s+": this.getSeconds(), // 秒
            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
            "S": this.getMilliseconds() // 毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    var soId = new Date().Format("yyyyMMddhhmmss");
    var createTime=new Date().Format("yyyy-MM-dd");
    var table=document.getElementById("soitemtablelist")
    var rows=table.rows;
    var soItemList=new Array()
    var productTotal=0;
    var tipFee=parseInt($("#tipFee").val())
    for(var i=1;i<rows.length;i++){
        var cells=rows[i].cells;
        var obj={}
        for(var j=1;j<cells.length;j++){
            var value=cells[j].childNodes[0].value
            var unitPrice=0;
            var num=0;
            obj["soId"]=soId
            if(j==1){
                obj["productCode"]=value
            }
            if(j==3){
                obj["unitName"]=value
            }
            if(j==4){
                obj["num"]=value
                num=value
            }
            if(j==6){
                obj["unitPrice"]=value
                unitPrice=value
            }
            obj["itemPrice"]=obj["unitPrice"]*obj["num"]
            if(j==7){
                soItemList[i-1]=obj
                productTotal+=obj["itemPrice"]
            }
        }
    }
    Ajax.post(
        '/addSomain',
        {
            soId:soId,
            createTime:createTime,
            customerName:$("#customerName111").val(),
            account:sessionStorage.getItem("account"),
            tipFee:tipFee,
            productTotal:productTotal,
            payType:$("#payType").val(),
            soTotal:tipFee+productTotal,
            prePayFee:$("#prePayFee").val(),
            remark:$("#remark").val(),
            soItemLists:JSON.stringify(soItemList)
        },
        function (result) {
            doResult(result,function () {
                show('so/somain.html')
                alert(result.msg)
            })
        }
    )

}
function updateSomain() {
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, // 月份
            "d+": this.getDate(), // 日
            "h+": this.getHours(), // 小时
            "m+": this.getMinutes(), // 分
            "s+": this.getSeconds(), // 秒
            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
            "S": this.getMilliseconds() // 毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    var createTime=new Date().Format("yyyy-MM-dd");
    var table=document.getElementById("soitemtablelist")
    var rows=table.rows;
    var soItemList=new Array()
    var productTotal=0;
    var tipFee=parseInt($("#tipFee").val())
    for(var i=1;i<rows.length;i++){
        var cells=rows[i].cells;
        var obj={}
        for(var j=1;j<cells.length;j++){
            var value=cells[j].childNodes[0].value
            var unitPrice=0;
            var num=0;
            obj["soId"]=$("#soId").val()
            if(j==1){
                obj["productCode"]=value
            }
            if(j==3){
                obj["unitName"]=value
            }
            if(j==4){
                obj["num"]=value
                num=value
            }
            if(j==6){
                obj["unitPrice"]=value
                unitPrice=value
            }
            obj["itemPrice"]=obj["unitPrice"]*obj["num"]
            if(j==7){
                soItemList[i-1]=obj
                productTotal+=obj["itemPrice"]
            }
        }
    }
    Ajax.post(
        '/updateSomain',
        {
            soId:$("#soId").val(),
            createTime:createTime,
            customerCode:$("#customerCode").val(),
            account:sessionStorage.getItem("account"),
            tipFee:tipFee,
            productTotal:productTotal,
            payType:$("#payType").val(),
            soTotal:tipFee+productTotal,
            prePayFee:$("#prePayFee").val(),
            remark:$("#remark").val(),
            soItemLists:JSON.stringify(soItemList)
        },
        function (result) {
            doResult(result,function () {
                show('so/somain.html')
                alert(result.msg)
            })
        }
    )
}
function tooneso() {
    show("/so/somainSettle.html")
}
function totwoso() {
    show("/so/somainSettle2.html")
}
function tothreeso() {
    show("/so/somainSettle3.html")
}